<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增消费记录')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-record-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">入住房间：</label>
                <div class="col-sm-8">
                    <select name="roomId" class="form-control" required>
                        <option th:each="room:${rooms}" th:value="${room.id}"
                                th:text="${room.number}" ></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">客人：</label>
                <div class="col-sm-8">
                    <select name="custmerId" class="form-control" required>
                        <option th:each="guest:${guests}" th:value="${guest.id}"
                                th:text="${guest.name}" ></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">服务项目：</label>
                <div class="col-sm-8">
                    <select name="serviceItemId" class="form-control" required onchange="updateItemPrice(this.value)">
                        <option th:each="serviceItem:${serviceItems}" th:value="${serviceItem.id}"
                                th:text="${serviceItem.name}" ></option>
                    </select>
                </div>
            </div>
        </div>
        <input name="itemPrice" id="itemPrice" type="hidden">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">消费数量：</label>
                <div class="col-sm-8">
                    <input name="quantity" class="form-control" type="text" required oninput="calculateTotalPrice()">
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">消费总金额：</label>
                <div class="col-sm-8">
                    <input name="totalPrice" class="form-control" type="text" required>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/consumption"
    $("#form-record-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-record-add').serialize());
        }
    }

    $("input[name='createdTime']").datetimepicker({
        format: "yyyy-MM-dd",
        minView: "month",
        autoclose: true
    });

    document.addEventListener('DOMContentLoaded', function () {
        var serviceItemSelect = document.getElementsByName('serviceItemId')[0];

        var selectedIndex = serviceItemSelect.selectedIndex;
        console.log("选中的索引：", selectedIndex);
        var selectedValue = serviceItemSelect.options[selectedIndex].value;
        console.log("选中的值：", selectedValue);
        updateItemPrice(selectedValue);
    });

    // 当服务项目下拉列表更改时更新 itemPrice
    function updateItemPrice(selectedServiceItemId) {
        var services = [[${serviceItems}]];
        for (var i = 0; i < services.length; i++) {
            if (Number(services[i].id) === Number(selectedServiceItemId)) {
                $("input[name='itemPrice']").val(services[i].price);
                break;
            }
        }
    }

    // 计算消费总金额
    function calculateTotalPrice() {
        console.log("计算消费总金额...");
        var itemPrice = parseFloat($("input[name='itemPrice']").val());
        console.log("项目价格：", itemPrice);
        var quantity = parseInt($("input[name='quantity']").val());
        console.log("数量：", quantity);
        if (!isNaN(itemPrice) &&!isNaN(quantity)) {
            var totalPrice = itemPrice * quantity;
            $("input[name='totalPrice']").val(totalPrice);
        }
    }
</script>
</body>
</html>